import DataBus   from '../databus'
import Sprite   from '../base/sprite'
import _COIGIG from '../runtime/mapdata'

//const ENEMY_IMG_SRC = 'images/enemy.png'
const NPC_WIDTH   = 24
const NPC_HEIGHT  = 24

const __ = {
  speed: Symbol('speed')
}

let databus7 = new DataBus()


export default class Enemy extends Sprite {
  constructor(id,orient,color,coord,vector,type,speed) {
    super("", 0, 0)
	this.orientation=orient
	this.oldorient=orient
	this.color=color
	this.vector=vector //下一步的坐标
	this.type=type
	this.speed=speed
    this.status=1		//对象状态,0表示未激活/结束,1表示正常,2表示暂停,3表示临时,4表示异常
	this._id=id;
	this.timeout =Math.floor(Math.random()*120) //定时器
    //this.initExplosionAnimation()
	var startpos=this.coord2position(coord.x,coord.y)
	this.x=startpos.x
	this.y=startpos.y //初始画布坐标
	
	this.width=NPC_WIDTH
	this.height=NPC_HEIGHT
	this.startcoord=coord ////初始地图位置坐标
	
	this.coord=coord
	this.times=0
	this.mapdata=_COIGIG[databus7.GAME_INDEX]['map']
  }
  
  

  setPlayer(player){
    this.player=player
  }

  
 
  update() {
    
	    var new_map;
	
	    
	    if(this.status==3&&!this.timeout){
		   this.status = 1;
	    }
	
	
	    if (this.timeout>0){ //开始睡眠的时间,或者变成灰色的时间
		  this.timeout--;
		  
	    }
		
		if (!databus7.gamestart)
		  return ;
	  
	    
	  
	    this.times++;
	
		var canvaspos={x:this.x,y:this.y};//画布坐标
				
   		var mappos= this.position2coord(this.x,this.y); 
	    this.coord=mappos;//自身的地图坐标值
	
	    if(this.status==1){
			if(!this.timeout){		//定时器
			    var str=JSON.stringify(this.mapdata)
				
				new_map = JSON.parse(str.replace(/2/g,"0"));
				var id = this._id;
				databus7.npcitems.forEach(function(item){
					if(item._id!=id&&item.status==1){	//NPC将其它所有还处于正常状态的NPC当成一堵墙,这样就会绕路走
						new_map[item.coord.y][item.coord.x]=1;
					}
				});
				
				
				this.path = this.finder({
						map:new_map,
						start:this.coord,
						end:this.player.coord
					});
				if(this.path.length){
					this.vector = this.path[0];
				}
					
				if (this.player.coord.x==this.coord.x && this.player.coord.y==this.coord.y){
					this.player.status=3; //玩家被吃
					databus7.gamestart=false
					return 
				}
					
			}
		}else if(this.status==3){//表示临时状态 (变灰色)
			new_map = JSON.parse(JSON.stringify(this.mapdata).replace(/2/g,"0"));
			var id = this._id;
			databus7.npcitems.forEach(function(item){
				if(item._id!=id){
					new_map[item.coord.y][item.coord.x]=1;
				}
			});
				
			this.path = this.finder({
					map:new_map,
					start:this.player.coord,
					end:this.coord,
					type:'next'
				});
				if(this.path.length){
					this.vector = this.path[Math.floor(Math.random()*this.path.length)];
				}
			
		}else if(this.status==4){ //(被吃)
			new_map = JSON.parse(JSON.stringify(this.mapdata).replace(/2/g,0));
			this.path = this.finder({ //回到初始位置
				map:new_map,
				start:this.coord,
				end:this.startcoord  // 本npc的初始位置 this._params.coord
			    
			});
			if(this.path.length){
				this.vector = this.path[0];
			}else{
				this.status = 1;
			}
		}
		//是否转变方向
		if(this.vector.change){ //遇到两边的出口通路,返回的方向值
			this.coord.x = this.vector.x;
			this.coord.y = this.vector.y;
			var pos = this.coord2position(this.coord.x,this.coord.y);
			this.x = pos.x;
			this.y = pos.y;
			console.log("change direction");
		}
		
		this.oldorient=this.orientation; //保存原有方向
	
		//方向判定
		if(this.vector.x>this.coord.x){
			this.orientation = 0;
		}else if(this.vector.x<this.coord.x){
			this.orientation = 2;
		}else if(this.vector.y>this.coord.y){
			this.orientation = 1;
		}else if(this.vector.y<this.coord.y){
			this.orientation = 3;
		}
		
		
		if (this.oldorient!=this.orientation){
			if (this.atcellcenter(canvaspos)==false)//如果在拐弯，需要判断是否在两个单元的中心
				this.orientation=this.oldorient;
		}
	
	    if (!databus7.mapmoving){//地图不移动，速度照旧
		   var edgeflag=this.atcelledge(canvaspos);
		   if (edgeflag==true){
			   if (this.orientation==2) //原左，碰到墙壁，向右
			      this.orientation=0;
			   else if (this.orientation==0)
                  this.orientation=2; 		
			   else if (this.orientation==1)
                  this.orientation=3; 		
			   else if (this.orientation==3)
				  this.orientation=1; 		
			  
		   }
		   
		   this.x += this.speed*databus7._COS[this.orientation];
		   this.y += this.speed*databus7._SIN[this.orientation];		   
		
		}else{ //地图移动
		   
		   var tempx=this.x;
		   var tempy=this.y;
		   this.x += this.speed*databus7._COS[this.orientation];
		   
		   if (this.player.orientation==1 && this.orientation==1 ) //两者向下
		      this.y -= (this.player.speed-this.speed);// ,databus7._SIN[this.orientation];
           else if (this.player.orientation==3 && this.orientation==3 ) //向上
			  this.y += (this.player.speed-this.speed); 
		   else if (this.player.orientation==3 && this.orientation==1 ) //player向上,npc 往下
		      this.y += (this.player.speed+this.speed); 
		   else if (this.player.orientation==1 && this.orientation==3 ) //向下,npc 往上
		      this.y -= (this.player.speed+this.speed);
		   else if (this.player.orientation==1) // player向下，enemy向左，或向右
			  this.y -= this.player.speed;
		   else if (this.player.orientation==3) // player向上
			  this.y += this.player.speed;
 		   else //player 向左，或右
			  this.y += this.speed*databus7._SIN[this.orientation];		   
		   
		   //console.log("moving:"+this.y);		   
		}
		
		
		if (this.status==3 && this.player.coord.x==this.coord.x && this.player.coord.y==this.coord.y){
			
			this.status=4; //status=4,enemy is killed
		}
   
   }
  
   render(context) {
	
      var isSick = false;
	  if(this.status==3){//被吃
		 isSick = this.timeout>0||this.times%2?true:false;
	  }
	  if(this.status!=4){
		  context.fillStyle = isSick?'#BABABA':this.color;
		  context.beginPath();
		  context.arc(this.x,this.y,this.width*.5,0,Math.PI,true);
		  switch(this.times%2){
			case 0:
			context.lineTo(this.x-this.width*.5,this.y+this.height*.4);
			context.quadraticCurveTo(this.x-this.width*.4,this.y+this.height*.5,this.x-this.width*.2,this.y+this.height*.3);
			context.quadraticCurveTo(this.x,this.y+this.height*.5,this.x+this.width*.2,this.y+this.height*.3);
			context.quadraticCurveTo(this.x+this.width*.4,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.4);
			break;
			case 1:
			context.lineTo(this.x-this.width*.5,this.y+this.height*.3);
			context.quadraticCurveTo(this.x-this.width*.25,this.y+this.height*.5,this.x,this.y+this.height*.3);
			context.quadraticCurveTo(this.x+this.width*.25,this.y+this.height*.5,this.x+this.width*.5,this.y+this.height*.3);
			break;
		 }
		 context.fill();
		 context.closePath();
	  }
	  context.fillStyle = '#FFF';
	  if(isSick){
		  context.beginPath();
		  context.arc(this.x-this.width*.15,this.y-this.height*.21,this.width*.08,0,2*Math.PI,false);
		  context.arc(this.x+this.width*.15,this.y-this.height*.21,this.width*.08,0,2*Math.PI,false);
		  context.fill();
		  context.closePath();
	  }else{
		  context.beginPath();
		  context.arc(this.x-this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false);
		  context.arc(this.x+this.width*.15,this.y-this.height*.21,this.width*.12,0,2*Math.PI,false);
		  context.fill();
		  context.closePath();
		  context.fillStyle = '#000';
		  context.beginPath();
		  context.arc(this.x-this.width*(.15-.04*databus7._COS[this.orientation]),this.y-this.height*(.21-.04*databus7._SIN[this.orientation]),this.width*.07,0,2*Math.PI,false);
		  context.arc(this.x+this.width*(.15+.04*databus7._COS[this.orientation]),this.y-this.height*(.21-.04*databus7._SIN[this.orientation]),this.width*.07,0,2*Math.PI,false);
		  context.fill();
		  context.closePath();
	  }
   }
   
   atcelledge(canvaspos){
	  var mapvector
	  if (this.orientation==2) //原左	
	      mapvector=this.coord2position(this.vector.x-1,this.vector.y)
	  else if (this.orientation==0) //原右
	      mapvector=this.coord2position(this.vector.x+1,this.vector.y)
	  else if (this.orientation==1) //原下
	      mapvector=this.coord2position(this.vector.x,this.vector.y)
	  else if (this.orientation==3) //原上
	      mapvector=this.coord2position(this.vector.x,this.vector.y)
	  
	  var left=mapvector.x
	  var right=mapvector.x+2*databus7.size
	  var topper=mapvector.y
	  var down=mapvector.y+2*databus7.size
	  
	  if (this.orientation==2){ //原左
	     if (this.get(this.vector.x-1,this.vector.y)==1 && Math.abs(left-canvaspos.x)<=15)
			 return true;
	  }else if (this.orientation==0){ //原右
	     if (this.get(this.vector.x+1,this.vector.y)==1 && Math.abs(left-canvaspos.x)<=15)
			 return true;
	  }else if (this.orientation==1){ //原下
	     if (this.get(this.vector.x,this.vector.y+1)==1 && Math.abs(down-canvaspos.y)<=4)
			 return true;
	  }else if (this.orientation==3){ //原上
	     if (this.get(this.vector.x,this.vector.y-1)==1 && Math.abs(topper-canvaspos.y)<=4)
	   		 return true;
	  }
	  
	  
	  return false;
   }
   
   atcellcenter(canvaspos){
	  
	  
	  var mapvector
	  if (this.oldorient==2) //原左	
	      mapvector=this.coord2position(this.vector.x-1,this.vector.y)
	  else if (this.oldorient==0) //原右
	      mapvector=this.coord2position(this.vector.x-1,this.vector.y)
	  else if (this.oldorient==1) //原下
	      mapvector=this.coord2position(this.vector.x,this.vector.y)
	  else if (this.oldorient==3) //原上
	      mapvector=this.coord2position(this.vector.x,this.vector.y)
	  
	  var left=mapvector.x
	  var right=mapvector.x+2*databus7.size
	  var topper=mapvector.y
	  var down=mapvector.y+2*databus7.size
	  
	  if (this.oldorient==2){ //原左
		  if (this.orientation==3 || this.orientation==1){//现上或下
			  if (canvaspos.x>=0.5*(left+right)+2 || canvaspos.x<=0.5*(left+right)-2){ //偏右
		     
		         return false; //需要继续left
	          }
			  else
			     return true;
		  
		  }
	  }
	  
	  if (this.oldorient==0){ //原右
		  if (this.orientation==3 || this.orientation==1){//现上或下
			  if (canvaspos.x>=0.5*(left+right)+2 || canvaspos.x<=0.5*(left+right)-2){ //偏右
		     
		         return false; //需要继续右
	          }
			  else
			     return true;
		  
		  }
	  }
	  
	  if (this.oldorient==1){ //原下
		  if (this.orientation==0 || this.orientation==2){//现左或右
			  if (canvaspos.y>=0.5*(topper+down)-10 || canvaspos.y<=0.5*(topper+down)-14){ //偏上
		     
		         return false; //需要继续向下
	          }
			  else
			     return true;
		  
		  }
	  }
	  
	  if (this.oldorient==3){ //原上
		  if (this.orientation==0 || this.orientation==2){//现左或右
			  if (canvaspos.y>=0.5*(topper+down)-10 || canvaspos.y<=0.5*(topper+down)-14){ //偏上
		     
		         return false; //需要继续向上
	          }
			  else
			     return true;
		  
		  }
	  }
	  
	  return true;
	  
	  
   
   }
   
  
}
